﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PluginDependencyTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            StringBuilder sb = new StringBuilder();

            var helps = new string[]
            {
                "使用说明：",
                "1：选择或输入动态完整连接后，点击下方测试按钮",
                "2：将要测试的动态名称输入到当前输入框，再点击下方测试按钮",
                "   若要使用方式2进行测试，请先清空第一个输入框中的内容，",
                "   动态库所依赖的C#动态库可通过ILSpy等工具获取，C++依赖可使用dumpbin等工具获取",
                "加载失败的原因：",
                "1. 直接依赖缺失：A.dll 不存在或路径不对。",
                "2. 间接依赖缺失：A.dll 需要 B.dll，但 B.dll 找不到。",
                "3. 版本不匹配：B.dll 的版本过旧或过新，与 A.dll 不兼容。",
            };

            foreach (var item in helps)
            {
                sb.AppendLine();
                sb.AppendLine(item);
            }

            textBox1.Text = sb.ToString();
            textBox1.Select(0, 0);
        }

        private void btn_csharp_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                if (string.IsNullOrEmpty(textBox1.Text))
                    return;

                AssemblyAnalyzer.Analysis(textBox1);
            }
            else
            {
                var analyzer = new HybridDependencyAnalyzer();
                var result = analyzer.AnalyzeHybridDependencies(textBox2.Text);

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("解析结果：");
                sb.AppendLine();
                foreach (var lib in result.ManagedDependencies)
                {
                    sb.AppendLine(lib.ToString());
                }
                sb.AppendLine();

                textBox1.Text = sb.ToString();
                textBox1.Text += AssemblyAnalyzer.Analysis(result.ManagedDependencies);
            }

            textBox1.Select(textBox1.Text.Length, 0);
            textBox1.ScrollToCaret();
        }

        private void btn_cpp_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text))
            {
                if (string.IsNullOrEmpty(textBox1.Text))
                    return;

                DllAnalyzer.Analysis(textBox1);
            }
            else
            {
                var analyzer2 = new DumpbinAnalyzer();
                var dependencies = analyzer2.GetNativeDependencies(textBox2.Text);

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("解析结果：");
                sb.AppendLine();
                foreach (var lib in dependencies)
                {
                    sb.AppendLine(lib);
                }
                sb.AppendLine();

                textBox1.Text = sb.ToString();
                textBox1.Text += DllAnalyzer.Analysis(dependencies);
            }

            textBox1.Select(textBox1.Text.Length, 0);
            textBox1.ScrollToCaret();
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            using (var openFileDialog = new OpenFileDialog())
            {
                // 配置单文件选择参数
                openFileDialog.Title = "选择插件动态链接库文件";
                openFileDialog.Filter = "动态链接库 (*.dll)|*.dll|所有文件 (*.*)|*.*";
                openFileDialog.Multiselect = false;
                openFileDialog.CheckFileExists = true;
                openFileDialog.InitialDirectory = Environment.GetFolderPath(
                    Environment.SpecialFolder.ProgramFilesX86);

                openFileDialog.InitialDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SSDP");

                textBox2.Text = openFileDialog.ShowDialog() == DialogResult.OK
                    ? openFileDialog.FileName
                    : string.Empty;
            }
        }
    }
}
